﻿Imports Mysoft.Map.Data
Imports System.Xml
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Web

Partial Public Class SelectAuditorData
    Inherits Mysoft.Map.Application.Security.AppPage

    Private xmlDoc As XmlDocument

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Public Sub ExecuteQuery()
        Me.RenderResults()
    End Sub


    Private Sub RenderResults()
        '单位或岗位 GUID
        Dim sGUID As String
        If Request.QueryString("GUID") Is Nothing Then
            sGUID = System.Guid.NewGuid().ToString()
        Else
            sGUID = Request.QueryString("GUID").ToString()
        End If

        Dim sType As String
        '0 单位 1 岗位
        If Request.QueryString("Type") Is Nothing Then
            sType = "0"
        Else
            sType = Request.QueryString("Type")
        End If
        '是否显示禁用用户 1显示,0不显示
        Dim sShowDisabledUser As String
        If Request.QueryString("ShowDisabledUser") Is Nothing Then
            sShowDisabledUser = "0"
        Else
            sShowDisabledUser = Request.QueryString("ShowDisabledUser").ToString()
        End If
        '查询字符串
        Dim searchValue As String
        If Request.QueryString("searchValue") Is Nothing Then
            searchValue = ""
        Else
            searchValue = Request.QueryString("searchValue").ToString()
        End If

        Dim dtTemp As DataTable = Me.GetUserData(sGUID, sShowDisabledUser, searchValue, sType)
        For i As Integer = 0 To dtTemp.Rows.Count - 1
            '行属性
            MyBase.Response.Write("<tr oid=""")
            MyBase.Response.Write(dtTemp.Rows(i)("GUID").ToString())
            MyBase.Response.Write(""">")

            '名字列
            MyBase.Response.Write("<td nowrap class=""sel"">")
            MyBase.Response.Write("<img src=""/_imgs/ico_16_16.gif"" class=""lui"">")
            MyBase.Response.Write(dtTemp.Rows(i)("Name").ToString())
            MyBase.Response.Write("</td>")

            '代码列
            MyBase.Response.Write("<td nowrap class=""sel"">")
            MyBase.Response.Write(dtTemp.Rows(i)("code").ToString())
            MyBase.Response.Write("</td>")

            MyBase.Response.Write("</tr>")
        Next

        If dtTemp.Rows.Count = 0 Then
            Me.RenderInlineMessage("没有找到记录，请重试。")
        End If
    End Sub

    Private Sub RenderInlineMessage(ByVal message As String)
        MyBase.Response.Write("<table class=""inlineMsg""><tr><td class=""inlineMsg"" align=""center"">")
        MyBase.Response.Write(message)
        MyBase.Response.Write("</td></tr></table>")
    End Sub

    '返回组织结构（单位和岗位）下的成员
    '参数说明：
    '   sGUID           单位GUID 或  岗位GUID
    '   sType           1表示岗位 0表示单位
    '   searchValue     查询值
    Private Function GetUserData(ByVal sGUID As String, ByVal sShowDisabledUser As String, ByVal searchValue As String, ByVal sType As String) As DataTable
        Dim SQL As String
        If sType = "0" Then  '单位
            SQL = "Declare @HierarchyCode varchar(1000),@BUGUID varchar(40),@BUType int;  " & _
                                        "Select @BUGUID = '" & sGUID.Replace("'", "''") & "'; " & _
                                        "Select @HierarchyCode = HierarchyCode,@BUType = BUType From myBusinessUnit Where BUGUID = @BUGUID;  " & _
                                        "If @BUType = 0	Begin  " & _
                                        "	 Select Distinct u.UserGUID as GUID,u.UserName as Name,5 as type,u.UserCode as code   " & _
                                        "	     	From myStation s   " & _
                                        "	     		Inner Join myBusinessUnit bu On s.BUGUID = bu.BUGUID   " & _
                                        "	     		Inner Join myStationUser su On s.StationGUID = su.StationGUID   " & _
                                        "	     		Inner Join myUser u On su.UserGUID = u.UserGUID   " & _
                                        "	     	Where " & IIf(sShowDisabledUser = "1", "(1=1) ", "(u.IsDisabeld=0 OR u.IsDisabeld is null) ") & _
                                        "				AND s.CompanyGUID =  @BUGUID   AND (u.userName + u.UserCode like '%" & searchValue.Replace("'", "''") & "%') " & _
                                        "	     	Order By u.UserCode,u.UserName  " & _
                                        "End  " & _
                                        "Else Begin " & _
                                        "	Select @HierarchyCode = @HierarchyCode + '.%';  " & _
                                        "	Select Distinct u.UserGUID as GUID,u.UserName as Name,5 as type,u.UserCode as code " & _
                                        "	     	From myStation s  " & _
                                        "	     		Inner Join myBusinessUnit bu On s.BUGUID = bu.BUGUID  " & _
                                        "	     		Inner Join myStationUser su On s.StationGUID = su.StationGUID  " & _
                                        "	     		Inner Join myUser u On su.UserGUID = u.UserGUID  " & _
                                        "	     	Where bu.HierarchyCode + '.'+ IsNull(s.HierarchyCode,'') Like @HierarchyCode  " & _
                                        "	     		AND " & IIf(sShowDisabledUser = "1", "(1=1) ", "(u.IsDisabeld=0 OR u.IsDisabeld is null) ") & _
                                        "               AND (u.userName + u.UserCode like '%" & searchValue.Replace("'", "''") & "%') " & _
                                        "	     	Order By u.UserCode,u.UserName  " & _
                                        "End  "

        Else    '岗位
            SQL = " SELECT Distinct u.UserGUID AS GUID,u.UserName AS Name,5 as type,u.UserCode as code" & _
                                       " FROM myUser u" & _
                                       " INNER JOIN myStationUser su ON su.UserGUID=u.UserGUID" & _
                                       " INNER JOIN myStation s ON su.StationGUID = s.StationGUID " & _
                                       " WHERE " & IIf(sShowDisabledUser = "1", "(1=1) ", "(u.IsDisabeld=0 OR u.IsDisabeld is null) ") & _
                                       " AND su.StationGUID='" & sGUID.Replace("'", "''") & "'" & _
                                       " AND (u.userName + u.UserCode like '%" & searchValue.Replace("'", "''") & "%') " & _
                                       " ORDER BY u.UserCode,u.UserName "
        End If

        Return MyDB.GetDataTable(SQL)
    End Function
End Class